package com.bdqn.controller;

import com.alibaba.fastjson.JSON;
import com.bdqn.constant.Constant;
import com.bdqn.pojo.SysRole;
import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysRoleService;
import com.bdqn.service.SysUserService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/**/role")
public class SysRoleController {
    private Logger logger = Logger.getLogger(SupplierController.class);
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysUserService sysUserService;
    @RequestMapping("/list")
    public String getRoleList(Model model){
        logger.info("进入角色查询方法");
        List<SysRole> roleList = null;
        roleList = sysRoleService.getRole();
        model.addAttribute("roleList",roleList);
        return "sysRole/list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "sysRole/add";
    }

    @PostMapping("/add")
    public String add(SysRole sysRole, Model model, HttpSession session){
        logger.info("进入修改页面");
        sysRole.setCreatedUserId(((SysUser)session.getAttribute(Constant.USER_SESSION)).getId());
        sysRole.setCreatedTime(new Date());
        logger.info("修改后的信息：" + sysRole);
        if (sysRoleService.roleAdd(sysRole)){
            return "redirect:sysRole/list";
        }
        return "sysRole/add";
    }

    @RequestMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable String id,Model model){
        logger.info("进入修改页面");
        SysRole sysRole = sysRoleService.roleById(id);
        logger.info("获取的信息：" + sysRole);
        model.addAttribute("sysRole",sysRole);
        return "sysRole/update";
    }

    @PostMapping("/update")
    public String update(SysRole sysRole,HttpSession session){
        logger.info("进入修改页面");
        sysRole.setCreatedUserId(((SysUser)session.getAttribute(Constant.USER_SESSION)).getId());
        sysRole.setCreatedTime(new Date());
        logger.info("修改后的信息：" + sysRole);
        if (sysRoleService.roleUpdate(sysRole)){
            return "redirect:role/list";
        }
        return "sysRole/update";
    }

    @ResponseBody
    @RequestMapping("/del/{id}")
    public Object del(@PathVariable String id){
        HashMap<String,Object> resultMap = new HashMap<String,Object>();
        logger.info("进入删除方法");
        logger.info("方法的id：" + id);
        if (sysRoleService.delRole(Integer.valueOf(id))){
            resultMap.put("delResult","true");
        }else {
            resultMap.put("delResult","false");
        }
        return JSON.toJSONString(resultMap);
    }
}
